package qcurepay

import (
    "fmt"
	"github.com/astaxie/beego"
	"thirdpaysdk/utils/mapstruct"
)

type PayInfo struct {
	TimeStamp  string `json:"timeStamp"`
	Package    string `json:"package"`
	PaySign    string `json:"paySign"`
	CoinType   string `json:"coin_type"`
	AppId      string `json:"appId"`
	Pg         string `json:"pg"`
	SignType   string `json:"signType"`
	NonceStr   string `json:"nonceStr"`
	RespCode   string `json:"respCode,omitempty"`
}

type MicroAppPayResponse struct {
	RespCode     string  `json:"respCode"`
	RespMsg      string  `json:"respMsg"`
	Version      string  `json:"version"`
	OrgCode      string  `json:"orgCode"`
	MerchCode    string  `json:"merchCode"`
	PayType      string  `json:"payType"`
	MerchOrderNo string  `json:"merchOrderNo"`
	OrderNo      string  `json:"orderNo"`
	Sign         string  `json:"sign"`
	PayInfo      string  `json:"payInfo"`
}

/*
{"merchCode":"000088880000289","merchOrderNo":"123456789102","orgCode":"66600196","payInfo":"{\"return_code\":\"FAIL\",\"return_msg\":\"appid and openid not match\"}","payType":"wxgjpay_public","respCode":"99","respMsg":"交易处理中","sign":"A1CC1D421F98203C22BE0720183E948B","version":"V1.0"}

{"merchCode":"QP0088880000005","merchOrderNo":"123456789106","orderNo":"HKDF937086251598331904","orgCode":"66600196","payInfo":"{\"timeStamp\":\"1553746882\",\"package\":\"prepay_id=wx28122122004968c45226c6ad0988602394\",\"paySign\":\"9CC2FCC57C9A66F67E08A53D7953FD6F\",\"coin_type\":\"HKD\",\"appId\":\"wx75e97ad177e2c05f\",\"pg\":\"prepay_id=wx28122122004968c45226c6ad0988602394\",\"signType\":\"MD5\",\"nonceStr\":\"jyZaSmicfyf5U59RSro5qbPEEICGuFGj\",\"respCode\":\"00\"}","payType":"wxgjpay_public","respCode":"00","respMsg":"成功","sign":"3632CD86707545C3AEF8818560288EB7","version":"V1.0"}

*/
func  MicroAppPay(orgCode,merchCode,appsecret,merchOrderNo,attach,subAppid,subOpenId,notifyUrl string,amount int)(rsp *MicroAppPayResponse, err error){
	reqURL := "http://www.qcurepay.com/trade/route"
	type PayReq struct {
		Version      string `json:"version"`
		OrgCode      string `json:"orgCode"`
		MerchCode    string `json:"merchCode"`
		SubOpenid    string `json:"sub_openid"`
		BizCode      string `json:"bizCode"`
		//MerchBiz   string `json:"merchBiz"`
		PayType      string `json:"payType"`
		MerchOrderNo string `json:"merchOrderNo"`
		SubAppid     string `json:"sub_appid"`
		Amount       int    `json:"amount"`
		GoodsName    string `json:"goodsName"`
		NotifyUrl    string `json:"notifyUrl"`
		Remark       string `json:"remark"`
	}
	req := new(PayReq)
	req.Version = "V1.0"
	req.OrgCode = orgCode
	req.MerchCode = merchCode
	req.SubOpenid = subOpenId
	req.BizCode = "01"
	//req.MerchBiz =
	req.PayType = "wxgjpay_public"
	req.MerchOrderNo = merchOrderNo
	req.SubAppid = subAppid
	req.Amount = amount
	req.GoodsName = attach
	req.NotifyUrl = notifyUrl
	req.Remark = attach
	//
	sdk := NewSDK(reqURL, appsecret)
	resp, err := sdk.Request(req)
	if err != nil {
	   beego.Debug("err:", err.Error())
	   return nil, err
	}
	if resp["respCode"] != "00"{
	   beego.Debug("error:", fmt.Sprintf("respCode=%s,respMsg=%s",resp["respCode"],resp["respMsg"]))
	   return nil, err
	}
	//beego.Debug("respMap", respMap)
	rsp = new(MicroAppPayResponse)
	err = mapstruct.Map2Struct(resp, rsp, "json")
	return
}
